import Loading from "./loading.vue";

/**
 * 全局插件形式注册组件
 * @param {Vue} vue
 * @param {String} loadingName
 */
export default function install(Vue) {
  let LoadingConstructor = Vue.extend(Loading);
  let instance;
  let loading = (Vue.prototype["$loadPop"] = {
    show(options = {}) {
      if (!instance) {
        instance = new LoadingConstructor().$mount(
          document.createElement("div")
        );
      }
      // console.log(instance.$el.innerHTML);
      if (instance.visable) return;
      instance.text =
        typeof options === "string" ? options : options.text || "";
      instance.customClass = options.customClass || "";
      document.body.appendChild(instance.$el);

      Vue.nextTick(() => {
        instance.visable = true;
      });
    },

    hide() {
      if (instance && instance.visable) {
        instance.visable = false;
      }
    }
  });

  Vue.mixin({
    watch: {
      "$route.path"() {
        loading.hide();
      }
    }
  });
}
